package com.situ.userWeb.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.userWeb.Service.IUserService;
import com.situ.userWeb.Service.Impl.UserServiceImpl;
import com.situ.userWeb.Util.JSONResult;
import com.situ.userWeb.Util.JSONUtil;
import com.situ.userWeb.entity.Department;
import com.situ.userWeb.entity.Role;
import com.situ.userWeb.entity.User;
import com.situ.userWeb.entity.UserRoleDepartment;

@WebServlet("/users")
@Controller
@RequestMapping("/users")
public class UsersServlet extends HttpServlet {
	private IUserService userService = new UserServiceImpl();

//	@Override
//	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		req.setCharacterEncoding("UTF-8");
//		String method = req.getParameter("method");
////		if (method == null) {
////			method = "selectAll";
////		}
//		switch(method) {
////		case "selectAll":
////			selectAll(req, resp);
////			break;
//		case "selectId":
//			selectId(req, resp);
//			break;
////		case "update":
////			update(req, resp);
////			break;
////		case "delete":
////			delete(req, resp);
////			break;
//		case "deleteAll":
//			deleteAll(req, resp);
//			break;
////		case "insert":
////			insert(req, resp);
////			break;
//		case "selectDepartmentRole":
//			selectDepartmentRole(req, resp);
//			break;
//		default:
//			break;
//		}
//	}
	@RequestMapping(value = "/selectDepartmentRole")
	private void selectDepartmentRole(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		ArrayList<Role> list1 = userService.selectRole();
		ArrayList<Department> list2 = userService.selectDepartment();

		req.setAttribute("roleList", list1);
		req.setAttribute("departmentList", list2);
		req.getRequestDispatcher("/user_insert.jsp").forward(req, resp);
	}

	@ResponseBody
	@RequestMapping(value = "/deleteAll")
	private JSONResult deleteAll(String ids) {
		System.out.println(ids);
		String[] idArray = ids.split(",");
		System.out.println(idArray);
		boolean isSuccess = userService.deleteAll(idArray);
		System.out.println(isSuccess);
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "删除失败");
		}
//		JSONUtil.obj2Json(jsonResult, resp);
		return jsonResult;
	}

	@ResponseBody
	@RequestMapping(value = "/update")
	private JSONResult update(User user, String[] roleId) {
//		String id = req.getParameter("id");
//		String name = req.getParameter("name");
//		String password = req.getParameter("password");
//		String email = req.getParameter("email");
//		String age = req.getParameter("age");
//		String departmentId = req.getParameter("departmentId");
//		User user = new User(Integer.parseInt(id), name, password, email, Integer.parseInt(age), Integer.parseInt(departmentId));
//		String[] roleId = req.getParameterValues("roleId");
		boolean isSuccess = userService.update(user, user.getId(), roleId);

		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "修改成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "修改失败");
		}
//		JSONUtil.obj2Json(jsonResult, resp);
		return jsonResult;
	}

	@RequestMapping(value = "/selectId")
	private void selectId(HttpServletRequest req, HttpServletResponse resp, Integer id)
			throws ServletException, IOException {
//		String userId = req.getParameter("id");
//		System.out.println(userId);
		System.out.println(id);
		User user = userService.selectId(id);
		ArrayList<Department> list2 = userService.selectDepartment();
		ArrayList<Role> list1 = userService.selectRole();
		List<Integer> selectRoleId = userService.selectRoleIdByUser(id);

		for (Integer integer : selectRoleId) {
			for (Role role : list1) {
				if (integer == role.getId()) {
					role.setChecked(true);
				}
			}
		}

		req.setAttribute("user", user);
		req.setAttribute("roleList", list1);
		req.setAttribute("departmentList", list2);
		req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
	}

	@ResponseBody
	@RequestMapping(value = "/insert")
	private JSONResult insert(User user, String[] roleId) throws IOException {
//		String id = req.getParameter("id");
//		String name = req.getParameter("name");
//		String password = req.getParameter("password");
//		String email = req.getParameter("email");
//		String age = req.getParameter("age");
//		String departmentId = req.getParameter("departmentId");
//		User user = new User(Integer.parseInt(id), name, password, email, Integer.parseInt(age), Integer.parseInt(departmentId));
		boolean isSuccessUser = userService.insert(user, roleId);
		System.out.println(isSuccessUser);

//		resp.sendRedirect(req.getContextPath() + "/users?method=selectAll");
		JSONResult jsonResult = null;
		if (isSuccessUser) {
			jsonResult = JSONResult.success("添加成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "添加失败");
		}
//		JSONUtil.obj2Json(jsonResult, resp);
		return jsonResult;
	}

	@ResponseBody
	@RequestMapping(value = "/delete")
	private JSONResult delete(Integer id) throws IOException {
//		String id = req.getParameter("id");
		System.out.println(id);
		boolean isSuccess = userService.delete(id);
		System.out.println(isSuccess);
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "删除失败");
		}
//		resp.sendRedirect(req.getContextPath() + "/users?method=selectAll");
//		JSONUtil.obj2Json(jsonResult, resp);
		return jsonResult;
	}

	@ResponseBody
	@RequestMapping(value = "/selectAll")
	private JSONResult selectAll(HttpServletRequest req, Integer page, Integer limit)
			throws ServletException, IOException {
//		List<UserRoleDepartment> list = userService.selectAll();
//		
//		req.setAttribute("list", list);
//		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
//		String page = req.getParameter("page");
//		String limit = req.getParameter("limit");
//		if (page == null || "".equals(page)) {
//			page = "1";
//		}
//		if (limit == null || "".equals(limit)) {
//			limit = "5";
//		}
		System.out.println(page);
		System.out.println(limit);
		JSONResult jsonResult = userService.selectLayuiByPage(page, limit);
		return jsonResult;
//		JSONUtil.obj2Json(jsonResult, resp);
	}

}
